<template>
  <div class="app-container">

    <el-form ref="param" :inline="true" :model="param" class="demo-form-inline">
      <el-form-item prop="name">
        <el-input v-model="param.name" placeholder="任务名称" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
        <el-button icon="el-icon-delete" @click="onClear('param')">清空</el-button>
        <el-button type="success" icon="el-icon-document-add" @click="add">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="加载中..."
      border
      fit
      highlight-current-row
      style="width: 100%">
      <el-table-column
        label="序号"
        type="index"
        width="50"
      />
      <el-table-column
        property="name"
        label="任务名称"
        width="120"
      />
      <el-table-column
        property="jobClass"
        label="执行类"
      />
      <el-table-column
        property="cronExpression"
        label="执行时间"
      />
      <el-table-column
        label="状态"
        width="100"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === 0">{{ scope.row.stateName }}</el-tag>
          <el-tag v-if="scope.row.state === 1" type="success">{{ scope.row.stateName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        width="180"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 10px">{{ scope.row.createDate }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="184"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-view"
            @click="show(scope.$index, scope.row)">
            查看
          </el-button>
          <el-dropdown trigger="click">
            <el-button type="info" size="mini">
              更多<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item icon="el-icon-caret-right" @click.native="trigger(scope.$index, scope.row)">执行</el-dropdown-item>
              <el-dropdown-item v-if="scope.row.state === 0" icon="el-icon-refresh-right" @click.native="resume(scope.$index, scope.row)">恢复</el-dropdown-item>
              <el-dropdown-item v-if="scope.row.state === 1" icon="el-icon-video-pause" @click.native="pause(scope.$index, scope.row)">停止</el-dropdown-item>
              <el-dropdown-item icon="el-icon-edit" @click.native="update(scope.$index, scope.row)">编辑</el-dropdown-item>
              <el-dropdown-item icon="el-icon-delete-solid" @click.native="remove(scope.$index, scope.row)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <br>

    <el-pagination
      background
      layout="total, sizes, prev, pager, next"
      :page-sizes="[10, 20, 30, 50, 100]"
      :current-page="param.pageNum"
      :total="page.total"
      @current-change="pageNumChange"
      @size-change="pageSizeChange"
    />

    <task-list-dialog ref="taskListDialog" :task-form="taskForm" @submitTaskForm="submitTaskForm" />

    <el-dialog
      title="任务详情"
      :visible.sync="infoDialogVisible"
    >
      <p>任务名称 : {{ task.name }}</p>
      <p>任务描述 : {{ task.description }}</p>
      <p>执行类 : {{ task.jobClass }}</p>
      <p>执行时间 : {{ task.cronExpression }}</p>
      <p>状态 : {{ task.stateName }}</p>
      <p>创建时间 : {{ task.createDate }}</p>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="infoDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { findList, addOrUpdate, deleteJob, trigger, resume, pause } from '@/api/task'
import TaskListDialog from './component/TaskListDialog.vue'
export default {
  components: {
    TaskListDialog
  },
  data() {
    return {
      infoDialogVisible: false,
      task: {},
      param: {
        pageNum: 1,
        pageSize: 10,
        name: ''
      },
      page: {
        total: 0
      },
      list: [],
      listLoading: true,
      taskForm: {
        id: '',
        name: '',
        description: '',
        jobClass: '',
        cronExpression: ''
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.listLoading = true
      findList(this.param).then(res => {
        this.list = res.data.list
        this.page.total = res.data.total
        this.listLoading = false
      })
    },
    filterTag(value, row) {
      return row.state === value
    },
    trigger(index, row) {
      trigger({ id: row.id }).then(res => {
        this.$message({
          message: res.msg,
          type: 'success'
        })
      })
    },
    resume(index, row) {
      resume({ id: row.id }).then(res => {
        this.$message({
          message: res.msg,
          type: 'success'
        })
        this.init()
      })
    },
    pause(index, row) {
      pause({ id: row.id }).then(res => {
        this.$message({
          message: res.msg,
          type: 'success'
        })
        this.init()
      })
    },
    submitTaskForm(taskFormValue) {
      addOrUpdate(taskFormValue).then(res => {
        this.$message({
          message: res.msg,
          type: 'success'
        })
        this.$refs.taskListDialog.dialogVisible = false
        // 刷新页面
        this.param.pageNum = 1
        this.init()
      })
    },
    show(index, row) {
      this.task = row
      this.infoDialogVisible = true
    },
    onSearch() {
      this.param.pageNum = 1
      this.init()
    },
    onClear(formName) {
      this.$refs[formName].resetFields()
    },
    pageNumChange(index) {
      this.param.pageNum = index
      this.init()
    },
    pageSizeChange(index) {
      this.param.pageSize = index
      this.init()
    },
    add() {
      this.$refs.taskListDialog.dialogVisible = true
    },
    addUser(userFormData) {
      addOrUpdate(userFormData).then(res => {
        this.$message({
          message: res.msg,
          type: 'success'
        })
        this.$refs.userDialog.dialogVisible = false
        // 刷新页面
        this.param.pageNum = 1
        this.init()
      })
    },
    update(index, row) {
      const job = this.list[index]

      this.taskForm.id = job.id
      this.taskForm.name = job.name
      this.taskForm.description = job.description
      this.taskForm.jobClass = job.jobClass
      this.taskForm.cronExpression = job.cronExpression

      this.$refs.taskListDialog.dialogVisible = true

      this.$nextTick(() => {
        this.userForm = this.taskForm
      })
    },
    remove(index, row) {
      this.$confirm('此操作将删除该任务, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteJob({ id: row.id }).then(res => {
          this.$message({
            message: res.msg,
            type: 'success'
          })
          this.init()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style>
  .el-dropdown {
    vertical-align: top;
    margin-left: 10px;
  }
  .el-dropdown + .el-dropdown {
    margin-left: 15px;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
</style>
